package com.wzdigit.wms.wms.service.core;

import com.wzdigit.wms.basic.domain.asn.AsnDtl;
import com.wzdigit.wms.basic.domain.asn.AsnHeader;
import com.wzdigit.wms.basic.client.request.asn.other.SearchOtherInOrderGoodsRequest;
import com.wzdigit.wms.basic.client.request.asn.other.SearchOtherInOrderRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.asn.other.OtherInOrderGoodsResponse;
import com.wzdigit.wms.basic.client.response.asn.other.OtherInOrderInfoResponse;
import com.wzdigit.wms.basic.mapper.core.order.manager.AsnManagerMapper;
import com.wzdigit.wms.wms.service.core.asn.AsnDtlGoodsService;
import com.wzdigit.wms.wms.service.core.asn.AsnDtlService;
import com.wzdigit.wms.wms.service.core.asn.AsnHeaderService;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * <p>
 * 收货单 服务类
 * </p>
 *
 * @author
 * @since 2021-01-12
 */
@Service
@Slf4j
public class AsnManagerService {

    @Autowired
    AsnManagerMapper asnManagerMapper;

    @Autowired
    AsnDtlGoodsService asnDtlGoodsService;
    @Autowired
    AsnDtlService asnDtlService;
    @Autowired
    AsnHeaderService asnHeaderService;

    public AsnDtlService getAsnDtlService() {
        return this.asnDtlService;
    }

    public AsnHeaderService getAsnHeaderService() {
        return this.asnHeaderService;
    }

    public AsnDtlGoodsService getAsnDtlGoodsService() {
        return this.asnDtlGoodsService;
    }

    /**
     * oqc单据下,已收过的sn信息
     *
     * @param listNo
     * @param listSn
     * @return
     */
    public Result existReceiveGoods(List<String> listOqcNo, List<String> listSn) {
        List<String> goodsSns = this.asnManagerMapper.selectExist(listOqcNo, listSn);
        List<String> existList = new ArrayList<>();
        for (String sn : listSn) {
            if (goodsSns.contains(sn))
                existList.add(sn);
        }
        if (existList.size() > 0)
            return new Result(false, String.join(",", existList));
        return new Result();
    }

    //按工单、产品、线体查询sn是否收货
    public List<String> searchIsReceive(String mfgLine,Integer skuId,String prdWo,List<String> snList){
        List<String> goodsSns=asnManagerMapper.searchIsReceive(mfgLine,skuId,prdWo,snList);
        return goodsSns;

    }

    /**
     * 新增 asn 头 + 1行明细
     *
     * @param asnHeader
     * @param asnDtl
     */
    public void addAsn(AsnHeader asnHeader, AsnDtl asnDtl) {
        asnHeader.setCreateTime(new Date());
        asnHeader.setModifyTime(asnHeader.getCreateTime());
        asnHeader = this.asnHeaderService.addNewOne(asnHeader);

        asnDtl.setAsnHeaderId(asnHeader.getAsnHeaderId());
        asnDtl.setCreateTime(new Date());
        asnDtl.setModifyTime(asnDtl.getCreateTime());
        asnDtl = this.asnDtlService.addNewOne(asnDtl);
    }


    public List<OtherInOrderGoodsResponse> selectGoodsByNo(SearchOtherInOrderGoodsRequest input) {
        return this.asnManagerMapper.selectGoodsByNo(input);
    }

    public List<OtherInOrderInfoResponse> selectOtherTaskInfo(SearchOtherInOrderRequest input) {
        if (input.getRangeWhsIds().size() == 0) {
            input.getRangeWhsIds().add(0);
        }
        return this.asnManagerMapper.selectOtherTaskInfo(input);
    }
}
